/**
 * Copyright (C) 2011 Denatics Development
 *
 * Programmed by David Tiersch <dtiersch@denatics.com>
 *
 * This file is part of B0mberman.
 *
 * B0mberman is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * B0mberman is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with B0mberman. If not, see <http://www.gnu.org/licenses/>.
 */
package com.denatics.bomberman;

import java.awt.SplashScreen;

import com.denatics.bomberman.control.KeyboardControl;
import com.denatics.bomberman.engine.Game;
import com.denatics.bomberman.gui.Screen;
import com.denatics.bomberman.tools.Debugger;
import com.denatics.bomberman.tools.Timer;

/**
 * A simple StartUp-Class to load the application.
 *
 * @author David Tiersch <dtiersch@denatics.com>
 */
public final class StartUp {

    /**
     * GUI.
     */
    private static Screen screen;

    /**
     * Engine of the Game.
     */
    private static Game game;

    /**
     * Timer which controls the ticks.
     */
    private static Timer timer;

    /**
     * Listener for the Keyboard.
     */
    private static KeyboardControl keyboardListener;

    /**
     * Empty, private constructor to forbid Creating an Instance of this class.
     */
    private StartUp() {
        // do nothing
    }

    /**
     * When launching the class, this will be automatically executed.
     *
     * @param args
     *            will not be interpreted
     */
    public static void main(final String[] args) {
        Debugger.printInfo("Program started");
        /*
         * maybe this becomes useful.
         * Ideas:
         * - When loading gets slower, show a loading bar
         */
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            Debugger.printInfo("no Splashscreen found");
        }

        screen = new Screen();

        timer = new Timer();
        timer.addListener(screen);

        keyboardListener = new KeyboardControl();
        screen.addKeyListener(keyboardListener);

        startGame(1);
    }

    /**
     * Starts the Game with the Appropriate number of Players.
     *
     * @param numberPlayers
     *            Number of Players
     */
    private static void startGame(final int numberPlayers) {
        timer.stop();
        game = new Game(numberPlayers);
        screen.setGame(game);
        keyboardListener.deleteObservers();
        keyboardListener.addObserver(game);

        screen.setVisible(true);
        timer.start();
    }
}
